<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<?php
function get_file_size($url)
{
	return filesize($url)?filesize($url):0;
}

function get_real_path($url)
{
	if(!$url) return '';
	if(strpos($url,"http://") === FALSE && strpos($url,'https://') === FALSE)
	{
		return ROOT_PATH.$url;
	}
	else
	{
		return $url;
	}
}

function siteurl($uri = '')
{
	$my_obj=& get_instance();
	if (is_array($uri))
	{
		$uri = implode('/', $uri);
	}

	if ($uri == '')
	{
		return ROOT_PATH.$my_obj->config->item('index_page');
	}
	else
	{
		$suffix = ($my_obj->config->item('url_suffix') == FALSE) ? '' : $my_obj->config->item('url_suffix');
		return ROOT_PATH.$my_obj->config->item('index_page').'/'.trim($uri, '/').$suffix; 
	}
}

function get_curren_url()
{
	$r_uri = '';
	if(isset($_SERVER["REQUEST_URI"]) && ! empty($_SERVER["REQUEST_URI"]))
	{
		$r_uri = $_SERVER["REQUEST_URI"];
	}
	else if(isset($_SERVER['PATH_INFO']) && ! empty($_SERVER["PATH_INFO"]))
	{
		$r_uri = $_SERVER['SCRIPT_NAME'].$_SERVER["PATH_INFO"];
		if($_SERVER['QUERY_STRING']) $r_uri .= '?' . $_SERVER['QUERY_STRING'];
	}
	$r_uri='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$r_uri;
	return urlencode($r_uri);
}

function getBytes($folder)
{
	$totalSize=0;
	$handle = opendir($folder) or die("Cannot open " . $folder);
	while($file = readdir($handle))
	{
	   if($file!="." && $file!="..")
	   {
		if(is_dir($folder."/".$file))
		{
		 //如果是目录，递归叠加大小
		 $totalSize=$totalSize+getBytes($folder."/".$file);
		}
		else
		{
		 //如果是文件
		 $totalSize=$totalSize+filesize($folder."/".$file);
		}
	   }
	}
	closedir($handle);
	return $totalSize;
}

function echo_msg($msg,$rdr='',$infotype='no',$target='_self')
{
	if(empty($rdr))
	{
		if (isset($_SERVER['HTTP_REFERER'])) $rdr=$_SERVER['HTTP_REFERER'];
		else $rdr="javascript:window.history.back();";
	}
	$tx_msg=array('infotype'=>$infotype,'infos'=>$msg,'red_url'=>$rdr,'target'=>$target);
	$my_obj=& get_instance();
	die($my_obj->load->view(TPL_FOLDER.'msg',$tx_msg,true));
}

function do_upload($up_config)
{
  if(is_array($up_config))
  {
  	foreach($up_config as $key=>$value)
	{
		$$key=$value;
	}
  }
  else
  {
  	return array('status'=>FALSE,'upload_errors'=>"<li>配置参数有误</li>");
  }
  $up_path=rtrim($up_path,'/');
  $up_path.='/'.date("Ymd",time()).'/';
  if (!file_exists($up_path))
  {
      mkdir($up_path);
   }
  $config['upload_path'] = $up_path;
  $config['allowed_types'] = $suffix;
  $config['encrypt_name'] =TRUE;
  
  $my_obj=& get_instance();
  $my_obj->load->library('upload');
  $my_obj->upload->initialize($config);
 
  if ( ! $my_obj->upload->do_upload($form_name))
  {
	if($_FILES[$form_name]['name'])
	{
		$file_data=array('status'=>FALSE,'upload_errors'=>$my_obj->upload->display_errors('<li>', '</li>'));
	}
	else
	{
		$file_data=array('status'=>TRUE,'file_path'=>'');
	}
	return $file_data;
  } 
  else
  {
   $data = $my_obj->upload->data();
   $file_data=array('status'=>TRUE,'file_path'=>$up_path.$data['file_name']);
   return $file_data;
  }
}

function unescape($str){ 
	$ret = ''; 
	$len = strlen($str); 
	for ($i = 0; $i < $len; $i++){ 
		if ($str[$i] == '%' && $str[$i+1] == 'u'){ 
			$val = hexdec(substr($str, $i+2, 4)); 
			if ($val < 0x7f) $ret .= chr($val); 
			else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f)); 
			else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f)); 
			$i += 5; 
		} 
		else if ($str[$i] == '%'){ 
			$ret .= urldecode(substr($str, $i, 3)); 
			$i += 2; 
		} 
		else $ret .= $str[$i]; 
	} 
	return $ret; 
} 

function escape($str)  
{
	preg_match_all("/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e",$str,$r);
	$str = $r[0];
	$l = count($str);
	for($i=0; $i<$l; $i++)
	{
		$value = ord($str[$i][0]);
		if($value < 223)
		{
			$str[$i] = rawurlencode(utf8_decode($str[$i]));
		}
		else
		{
			$str[$i] = "%u".strtoupper(bin2hex(iconv("UTF-8","UCS-2",$str[$i])));
		}
	}
	return join("",$str);
}

function strcut($string, $length, $dot = '…',$charset='utf-8')
{
	$strlen = strlen($string);
	if($strlen <= $length) return $string;
	$strcut = '';
	if(strtolower($charset) == 'utf-8')
	{
		$n = $tn = $noc = 0;
		while($n < $strlen)
		{
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} 
			elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} 
			elseif(224 <= $t && $t <= 239) {
				$tn = 3; $n += 3; $noc += 3;
			} 
			elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 4;
			} 
			elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 5;
			} 
			elseif($t == 252 || $t == 253) 
			{
				$tn = 6; $n += 6; $noc += 6;
			}
			else 
			{
				$n++;
			}
			if($noc >= $length) break;
		}
		if($noc > $length) $n -= $tn;
		$strcut = substr($string, 0, $n);
	}
	else
	{
		$dotlen = strlen($dot); 
		$maxi = $length - $dotlen - 1;
		for($i = 0; $i < $maxi; $i++)
		{
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}
	return $strcut;
}

function format_curren($v)
{
	if(is_numeric($v)) return '￥'.number_format($v,2);
	else return '￥'.number_format(0,2);
}

function replace_htmlAndjs($document)
{
	$document = trim($document);
	if (strlen($document) <= 0)
	{
	  return $document;
	}
	$search = array ("'<script[^>]*?>.*?</script>'si",  // 去掉 javascript
					  "'<[\/\!]*?[^<>]*?>'si",          // 去掉 HTML 标记
					  "'([\r\n\t])[\s]+'",                // 去掉空白字符
					  "'&(quot|#34);'i",                // 替换 HTML 实体
					  "'&(amp|#38);'i",
					  "'&(lt|#60);'i",
					  "'&(gt|#62);'i",
					  "'&(nbsp|#160);'i"
			  );// 作为 PHP 代码运行
	
	$replace = array ("", "","","\"","&","<",">"," ");
	return @preg_replace ($search, $replace, $document);
}

function format_textarea($str)
{
	if(empty($str)) return '';
	$str = str_replace("\r\n",'<br />',$str);
	$str = str_replace("\n",'<br />',$str);
	$str = str_replace("\r",'<br />',$str);
	$str = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',$str);
	return $str;
}

function time_diff($t)
{
	$n = time();
	$dif = ($n - $t) * 1000;
	$rstr = '';
	if(($difv = floor($dif / 31536000000)) >= 1)
	{
		$rstr = $difv . '年前';
	}
	elseif(($difv = floor($dif / 2592000000)) >= 1)
	{
		$rstr = $difv . '月前';
	}
	elseif(($difv = floor($dif / 86400000)) >= 1)
	{
		$rstr = $difv . '天前';
	}
	elseif(($difv = floor($dif / 3600000)) >= 1)
	{
		$rstr = $difv . '小时前';
	}
	elseif(($difv = floor($dif / 60000)) >= 1)
	{
		$rstr = $difv . '分钟前';
	}
	elseif(($difv = floor($dif / 1000)) >= 1)
	{
		$rstr = $difv . '秒前';
	}
	return $rstr;
}
?>
